/// <reference types="vitest" />
import path from 'path'
import { defineConfig, loadEnv } from 'vite'
import type { ConfigEnv } from 'vite'
import { createVitePlugins } from './build/plugins'

// https://vitejs.dev/config/
const viteConfig = defineConfig((mode: ConfigEnv) => {
  const root = process.cwd()
  const env = loadEnv(mode.mode, root)
  const { VITE_PORT } = env
  return {
    plugins: createVitePlugins(mode),
    test: {
      environment: 'happy-dom',
    },
    define: {
      'process.env': {}, // 将上面的env赋值到这里
    },
    server: {
      host: true,
      port: VITE_PORT,
      proxy: {
        '/api': {
          target: env.VITE_BASEURL, // 目标服务器地址
          ws: true, // 是否启用 WebSocket
          changeOrigin: true, // 是否修改请求头中的 Origin 字段
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
    root: process.cwd(), // 项目根目录
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@yang': path.resolve(__dirname, './src/library'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@/theme/index.scss";',
        },
      },
    },
  }
})

export default viteConfig
